/**
 * @(#)EmailUtils.java 2017年4月7日
 * Copyright(C) 2017年4月7日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.util;

import java.io.File;
import java.io.IOException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.common.utils.StringUtils;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */
public class EmailSslUtils {

	public static final Logger LOGGER = LoggerFactory.getLogger(EmailSslUtils.class);

	String[] to = {"396340209@qq.com"};
	String[] cc = {};

	String from = "qiyigame1@163.com";
	String passWord = "qiyigame";

	String host = "smtp.163.com";
	String port = "465";
	String subject = "监控";

	String[] attchmentFileList = null;

	String content = "This email is from system, please don't reply!";
	
	private String mailDebug="false";

	public void send() throws IOException {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		// Get a Properties object
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", host);
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", port);
		props.setProperty("mail.smtp.socketFactory.port", port);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", mailDebug);
		props.put("mail.transport.protocol","smtp");
		

		final String username = this.from;
		final String password = passWord;
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
		try {
			// 创建邮件
			Message msg = new MimeMessage(session);

			// 设置发件人和收件人
			msg.setFrom(new InternetAddress(from));

			Address[] addArr =  new InternetAddress[this.to.length];
			for(int i=0;i<this.to.length;i++){
				  addArr[i] = new InternetAddress(this.to[i]);
			}
			
			msg.setRecipients(Message.RecipientType.TO, addArr);
			
			
			if (this.cc != null&&this.cc.length>0) {
				Address[] ccArr =  new InternetAddress[this.cc.length];
				for(int i=0;i<this.cc.length;i++){
					ccArr[i] = new InternetAddress(this.cc[i]);
				}
				msg.setRecipients(Message.RecipientType.CC, ccArr);
			}
			
			msg.setSubject(this.subject);
			msg.setSentDate(new Date());

			Multipart mp = new MimeMultipart();

			MimeBodyPart htmlPart = new MimeBodyPart();
			htmlPart.setContent(this.content, "text/html;charset=utf8");
			mp.addBodyPart(htmlPart);

			if (this.attchmentFileList != null) {
				for (String attach : this.attchmentFileList) {
					if(StringUtils.isBlank(attach)){
						continue;
					}
					String fileName=attach;
					int idx=attach.lastIndexOf(File.separator);
					if(idx>0){
						fileName=attach.substring(idx+1);
					}
					
					MimeBodyPart attachmentPart = new MimeBodyPart();  
	                FileDataSource source = new FileDataSource(attach);  
	                attachmentPart.setDataHandler(new DataHandler(source));  
	                attachmentPart.setFileName(MimeUtility.encodeWord(fileName, "gb2312", null));  
	                mp.addBodyPart(attachmentPart); 
					
				}
			}

			msg.setContent(mp);

			Transport  transport = session.getTransport();
			transport.connect();
			transport.sendMessage(msg,msg.getAllRecipients());
			transport.close();
			
//			Transport.send(msg);
		} catch (MessagingException mex) {
			LOGGER.error("",mex);
			mex.printStackTrace();
		}
	}

	public String[] getTo() {
		return to;
	}

	public void setTo(String[] to) {
		this.to = to;
	}

	public String[] getCc() {
		return cc;
	}

	public void setCc(String[] cc) {
		this.cc = cc;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String[] getAttchmentFileList() {
		return attchmentFileList;
	}

	public void setAttchmentFileList(String[] attchmentFileList) {
		this.attchmentFileList = attchmentFileList;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public String getMailDebug() {
		return mailDebug;
	}

	public void setMailDebug(String mailDebug) {
		this.mailDebug = mailDebug;
	}

	public static void main(String[] args) throws AddressException, MessagingException, IOException{
		EmailSslUtils email=new EmailSslUtils();
		String[] attachArr=new String[1];
		attachArr[0]="E:\\test\\嘿嘿嘿.xls";
		email.setAttchmentFileList(attachArr);
		email.setContent("<div style=\"font-size:x-large;text-align:center\"><b>模拟器数量监控</b><div><br><div style=\"font-size:80%;text-align:left;\">1.2.3版本：对应的模拟器至少还需要0个，实际在线模拟器为0个，空闲模拟器为:0个</div><br><div style=\"font-size:80%;text-align:left;\">1.0版本：对应的模拟器至少还需要0个，实际在线模拟器为0个，空闲模拟器为:0个</div><br><hr><table border=\"1\"><thead><tr><th width=\"20%\" align=\"left\">应用名称</th><th width=\"10%\" align=\"left\">当前剩余任务量</th><th width=\"10%\" align=\"left\">新增任务数</th><th width=\"10%\" align=\"left\">留存任务数</th><th width=\"10%\" align=\"left\">已执行量</th><th width=\"10%\" align=\"left\">30分钟执行量(新增|留存)</th><th width=\"10%\" align=\"left\">已执行占比</th><th width=\"15%\" align=\"left\">提示</th></tr></thead><tbody><tr><td>音乐翼锋</td><td>0</td><td>2000|0|0</td><td>0|0|0</td><td>0</td><td></td><td></td><td></td></tr><tr><td>阅读-学高-test</td><td>0</td><td>10|0|0</td><td>0|0|0</td><td>0</td><td></td><td></td><td></td></tr></tbody></table><br><hr><br>");
		email.send();
	}
}
